package umn.angrypenguins.monopoly.utils;

import umn.angrypenguins.monopoly.models.GameInstance;
import umn.angrypenguins.monopoly.models.PlayerInstance;

import java.util.ArrayList;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * Created with IntelliJ IDEA.
 * User: Chris Seeling
 * Date: 5/1/12
 * Time: 11:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class JsonSerializer {

    public static ArrayList<GameInstance> parseGames(String json) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray array = parser.parse(json).getAsJsonArray();
        ArrayList<GameInstance> games = new ArrayList<GameInstance>();
        for(JsonElement game : array) {
            GameInstance gi = gson.fromJson(game, GameInstance.class);
            games.add(gi);
        }
        return games;
    }

    public static GameInstance parseGameInstance(String json) {
        Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", "parsing game, json = " + json);
        try {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(json).getAsJsonObject();
            JsonArray playersArray = jsonObject.getAsJsonArray( "players" );

            GameInstance game = gson.fromJson(jsonObject, GameInstance.class);
            Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", "game name = " + game.getName());
            Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", "player array = " + playersArray.toString());
            ArrayList<PlayerInstance> players = parsePlayers(playersArray.toString());
            for(PlayerInstance player : players) {
                if(player.isGameCreator()) {
                    game.setCreator(player);
                }
            }
            game.setPlayers(players);

            return game;
        }
        catch (Exception ex) {
            Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", ex.getMessage() + "\n" + ex.getStackTrace());
            return  null;
        }
    }

    public static ArrayList<PlayerInstance> parsePlayers(String json) {
        Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", "parsing players");
        Log.i("MonopolyMoney.JsonSerializer.parseGameInstance", "incoming json = " + json);
        try {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonArray array = parser.parse(json).getAsJsonArray();
            ArrayList<PlayerInstance> players = new ArrayList<PlayerInstance>();
            for(JsonElement player : array) {
                PlayerInstance pi = gson.fromJson(player, PlayerInstance.class);
                players.add(pi);
            }
            return players;
        }
        catch (Exception ex) {
            Log.i("MonopolyMoney.JsonSerializer.parsePlayers", ex.getMessage() + "\n" + ex.getStackTrace());
            return  null;
        }
    }

    public static PlayerInstance parsePlayerInstance(String json) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(json).getAsJsonObject();
        //JsonObject contactJsonObject = jsonObject.getAsJsonObject( "player" );

        PlayerInstance player = gson.fromJson(jsonObject, PlayerInstance.class);

        return player;
    }
}
